import { BasicColumn, FormSchema } from '@/components/Table';
import { h } from 'vue';
import { Switch } from 'ant-design-vue';
import { useMessage } from '@/hooks/web/useMessage';
import { setRoleStatus } from '@/api/demo/system';

export const columns: BasicColumn[] = [
  {
    title: '类目名称',
    dataIndex: 'deptName',
    align: 'left',
  },
  {
    title: '栏目缩写',
    dataIndex: 'pinyin',
    helpMessage:'栏目全拼首字母缩写',
    width: 180,
  },
  {
    title: '栏目首字母',
    dataIndex: 'py',
    helpMessage:'栏目首字母缩写',
    width: 180,
  },
  {
    title: '排序',
    dataIndex: 'orderNo',
    width: 80,
    sorter: true,
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 100,
    sorter: true,
    customRender: ({ record }) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.status === '1',
        checkedChildren: '停用',
        unCheckedChildren: '启用',
        loading: record.pendingStatus,
        onChange(checked: CheckedType) {
          record.pendingStatus = true;
          const newStatus = checked ? '1' : '0';
          const { createMessage } = useMessage();
          setRoleStatus(record.id, newStatus)
            .then(() => {
              record.status = newStatus;
              createMessage.success(`已成功修改角色状态`);
            })
            .catch(() => {
              createMessage.error('修改角色状态失败');
            })
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  },
  {
    title: '创建时间',
    dataIndex: 'id',
    width: 180,
    sorter: true,
    fixed: 'right',
    customRender: ({record})=>{
      return record.createTime;
    }
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'name',
    label: '类目名称',
    component: 'Input',
    colProps: { xs: 24, xl: 12, lg: 8, xxl: 6 },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'parentDept',
    label: '上级类目',
    component: 'TreeSelect',
    ifShow({ values }) {
      const { deptName, parentDept } = values;
      // Hide without a parentDept when editing
      return parentDept || (!deptName && !parentDept);
    },
    componentProps: {
      fieldNames: {
        label: 'deptName',
        value: 'id',
      },
      getPopupContainer: () => document.body,
    },
    required: false,
  },
  {
    field: 'name',
    label: '类目名称',
    component: 'Input',
    required: true,
  },
  {
    label: '拼音缩写',
    field: 'pinyin',
    helpMessage:'栏目全拼首字母缩写',
    required: true,
    component: 'Input',
    colProps: { xs: 24, md: 12 },
  },
  {
    label: '首字母',
    field: 'py',
    helpMessage:'栏目首字母缩写',
    required: true,
    component: 'Input',
    colProps: { xs: 24, md: 12 },
  },
  {
    field: 'orderNo',
    label: '排序',
    component: 'InputNumber',
    required: true,
  },
  {
    field: 'status',
    label: '状态',
    component: 'RadioButtonGroup',
    defaultValue: '0',
    componentProps: {
      options: [
        { label: '启用', value: '0' },
        { label: '停用', value: '1' },
      ],
    },
    required: true,
  },
  {
    label: '备注',
    field: 'remark',
    component: 'InputTextArea',
  },
];
